package com.hadoop.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;
import java.util.StringTokenizer;


/**
 * Created by Administrator on 2020/5/5.
 */
public class MrDebug {

    /**
     * mapper
     */
    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        @Override
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

            StringTokenizer stk = new StringTokenizer(value.toString());

            while (stk.hasMoreTokens()) {
                word.set(stk.nextToken());
                context.write(word, one);
            }
        }

    }

    /**
     * reducer
     */
    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        private final static IntWritable sum = new IntWritable(1);

        @Override
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

            int temp = 0;
            for (IntWritable val : values) {
                temp += val.get();
            }
            sum.set(temp);
            context.write(key, sum);
        }
    }

    private static void deleteOutput(Configuration conf, String path) throws IOException {
        try (FileSystem fs = FileSystem.get(conf)) {
            Path outPath = new Path(path);
            if (fs.exists(outPath)) {
                boolean delResult = fs.delete(outPath, true);
                if (delResult) {
                    System.out.println(path + " deleted.");
                } else {
                    System.out.println(path + " delete failed.");
                }
            }
        }

    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        System.out.println("begin");
        Configuration conf = new Configuration();

        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

        if (otherArgs.length < 2) {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }

        deleteOutput(conf, otherArgs[otherArgs.length - 1]);

        Job job = Job.getInstance(conf, "mrdebug");
        job.setJarByClass(MrDebug.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        for (int i = 0; i < otherArgs.length - 1; i++) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }


}
